<template>
  <div>
    <h3>员工列表，欢迎{{ user.name }}</h3>
    <el-button type="primary" @click="dialogFormVisible = true" plain>添加用户</el-button>
    <!--    添加用的Dialog对话框-->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible" :width="dialogWidth">
      <el-form :model="insert">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input placeholder="请输入用户名" v-model="insert.name"></el-input>
        </el-form-item>

        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input placeholder="请输入密码" v-model="insert.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-radio-group v-model="insert.sex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="出生日期" :label-width="formLabelWidth">
          <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="insert.birthday"
                          style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>


<!--  查看用户的对话框-->
    <el-dialog title="查看用户信息" :visible.sync="dialogUserLook" :width="dialogWidth">
      <el-form :model="insert">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input placeholder="请输入用户名" v-model="userLook.name" :disabled="true"></el-input>
        </el-form-item>

        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input placeholder="请输入密码" v-model="userLook.password"  :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-radio-group v-model="userLook.sex" :disabled="true">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="出生日期" :label-width="formLabelWidth">
          <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="userLook.birthday"
                          style="width: 100%;" :disabled="true"></el-date-picker>
        </el-form-item>
        <el-form-item label="出生日期" :label-width="formLabelWidth">
          <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="userLook.registTime"
                          style="width: 100%;" :disabled="true"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogUserLook = false">确 定</el-button>
      </div>
    </el-dialog>

    <!--    显示页面表格数据-->
    <el-table
      :data="userList.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
      style="width: 100%">
      <el-table-column
        label="ID"
        prop="id">
      </el-table-column>
      <el-table-column
        label="名字"
        prop="name">
      </el-table-column>
      <el-table-column
        label="性别"
        prop="sex">
      </el-table-column>
      <el-table-column
        label="出生日期"
        prop="birthday">
      </el-table-column>
      <el-table-column
        label="注册日期"
        prop="registTime">
      </el-table-column>
      <el-table-column
        align="right">
        <template slot="header" slot-scope="scope">
          <el-input
            v-model="search"
            size="mini"
            placeholder="输入关键字搜索"/>
        </template>
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleLook(scope.$index, scope.row)" >查看
          </el-button>
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)" icon="el-icon-edit">编辑
          </el-button>

          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row ) " icon="el-icon-delete">删除
          </el-button>

        </template>
      </el-table-column>


    </el-table>
<!--    分页-->
    <div class="block">
      <!--
      @size-change    pageSize(每页的数量) 改变时会触发
      @current-change  pageNum(当前第几页) 改变时会触发
      page-size 每页显示条目个数
      page-sizes  每页显示个数选择器的选项设置
      layout 组件布局，子组件名用逗号分隔
          total 共多少条数据
          sizes 选择一页多少条数据
          prev 上一页
          pager 当前页
          next 下一页
          jumper 输入框前往第几页
      total 总数据
      -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.pageNum"
        :page-size="pageInfo.pageSize"
        :page-sizes="[1, 2, 5, 10]"
        layout="total, sizes, prev, pager, next ,jumper"
        :total="pageInfo.total">
      </el-pagination>
    </div>


  </div>
</template>

<script>
import Qs from 'qs'

export default {
  name: "Home",
  data() {
    return {
      success: "Home页面",
      //获取登录成功的用户信息
      user: this.$store.state.user,
      //页面显示的用户信息
      userList: [],
      //搜索框，只能搜索当前页面的信息
      search: '',
      // 分页数据
      pageInfo: {
        pageNum:1,  //默认第一页
        pageSize:5  //默认五条数据
      },
      //用于添加的Dialog的对话框
      dialogFormVisible: false,
      dialogUserLook:false,
      insert: {
        name: '',
        password: '',
        sex: '',
        birthday: '',

      },
      formLabelWidth: '80px',
      dialogWidth: '30%',
      userLook:{}
    }
  },
  //页面挂载事件
  mounted() {
    //页面挂载后执行查询功能
    this.showAll();
  },
  methods: {
    //查询用户信息，分页功能
    showAll() {
      let vm = this;
      this.axios({
        url: `/localhost/vue/showAll?pageIndex=${vm.pageInfo.pageNum}&pageSize=${vm.pageInfo.pageSize}`,
        method: "get",
      }).then(function (res) {
        //获取分页函数
        vm.pageInfo = res.data.data;
        //获取员工数据
        vm.userList = res.data.data.list;
      });
    },
    //用于修改跳转页面
    handleEdit(index, row) {
      console.log(index, row.id);
      let vm = this;
      vm.$router.push(`/Update/${row.id}`)
    },
    //删除功能
    handleDelete(index, row) {
      //提示框
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //点击确认  请求后端数据
        let vm = this;
        this.axios({
          url: `/localhost/vue/delete/${row.id}`,
          method: "delete"
        }).then(function (res) {
          if (res.data.code == 0) {
            vm.showAll();
            vm.$message.success("删除成功");
          }
        }).catch(function (err) {
          console.log(err);
          vm.$message.error("删除失败");
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    //添加Dialog确认按钮功能，如果确认执行添加功能
    add() {
      let vm = this;
      this.axios({
        url: '/localhost/vue/insert',
        method: 'post',
        data: {
          user: vm.insert
        },
        transformRequest: [function (data) {
          console.log(data.user)
          return Qs.stringify(data.user);
        }]
      }).then(function (res) {
        console.log(res.data)
        if (res.data.code == 0) {
          vm.$message.success("添加成功");
          vm.dialogFormVisible = false;
        }
      }).catch(function (err) {
        console.log(err)
      });
    },
    //监听 pagesize 改变的事件
    handleSizeChange(newsize) {
      //这里conso 选中第几页 最新的值
      console.log("handleSizeChange"+newsize)
      //最新的条数（newsize）赋值给 动态的 pagesie
      this.pageInfo.pageSize = newsize
      //获取到最新一页显示的数据  重新发送axios请求 这里是封装好的请求方法

      this.showAll();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log("handleCurrentChange"+newPage)
      //把最新的页码（newPage）赋值给 动态的 pagenum
      this.pageInfo.pageNum = newPage
      //获取到最新显示的页码值  重新发送axios请求 这里是封装好的请求方法
      this.showAll()
    },

    handleLook(index,row){
      this.userLook = row;
      this.dialogUserLook=true
    }

  },
}
</script>

<style scoped>

</style>
